﻿using AuthCenter.API.Application.Command.AccountRole;
using AuthCenter.API.DTOs;
using AuthCenter.Domain.Account;
using AuthCenter.ErrorCode;
using AuthCenter.Intertructrue.Interface;
using MediatR;

namespace AuthCenter.API.Application.Handler.AccountRole
{
    public class GetRoleUserHandler : IRequestHandler<GetRoleUserCommand, APIResult<List<Tree>>>
    {
        private readonly IBaseRepository<AccountModel> accountRepo;

        public GetRoleUserHandler(IBaseRepository<AccountModel> accountRepo)
        {
            this.accountRepo = accountRepo;
        }
        public Task<APIResult<List<Tree>>> Handle(GetRoleUserCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<Tree>> result = new APIResult<List<Tree>>();
            var res = accountRepo.GetAll().Where(x=>x.Department == request.DepartmentId&&!x.IsDel).ToList();
            var tree = new List<Tree>();
            foreach (var item in res)
            {
                tree.Add(new Tree()
                {
                    id = item.Id,
                    label = item.Name,
                });
            }
            result.Message = "显示成功";
            result.Code = APIEnum.Ok;
            result.Data = tree;
            return Task.FromResult(result);
        }
    }
}
